﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Threading;

namespace DusonFramework.Utils.Mail
{
    public class DotNetMail : EmailProvider
    {
        public override bool Send(string to, string from, string subject, string message)
        {
            MailMessage mailMessage = new MailMessage(from, to, subject, message);
            this.SendMailMessage(mailMessage, null);

            return true;
        }

        /// <summary>
        /// Sends a MailMessage object using the SMTP settings.
        /// </summary>
        public void SendMailMessage(MailMessage message, string[] paths)
        {
            if (message == null)
                throw new ArgumentNullException("message");

            try
            {
                message.IsBodyHtml = true;
                message.BodyEncoding = Encoding.UTF8;
                SmtpClient smtp = new SmtpClient(this.SmtpServer);
                smtp.Credentials = new System.Net.NetworkCredential(this.UserName, this.Password);
                smtp.Port = this.Port;
                smtp.EnableSsl = true;

                //添加附件
                if (null != paths && paths.Length != 0)
                {
                    foreach (string path in paths)
                    {
                        Attachment data = new Attachment(path, System.Net.Mime.MediaTypeNames.Application.Octet);
                        message.Attachments.Add(data);
                    }
                }

                smtp.Send(message);
                OnEmailSent(message);
            }
            catch (SmtpException)
            {
                OnEmailFailed(message);
            }
            finally
            {
                // Remove the pointer to the message object so the GC can close the thread.
                message.Dispose();
                message = null;
            }
        }

        /// <summary>
        /// Sends the mail message asynchronously in another thread.
        /// </summary>
        /// <param name="message">The message to send.</param>
        public void SendMailMessageAsync(MailMessage message)
        {
            ThreadPool.QueueUserWorkItem(delegate { this.SendMailMessage(message, null); });
        }

        /// <summary>
        /// Occurs after an e-mail has been sent. The sender is the MailMessage object.
        /// </summary>
        public event EventHandler<EventArgs> EmailSent;
        private void OnEmailSent(MailMessage message)
        {
            if (EmailSent != null)
            {
                EmailSent(message, new EventArgs());
            }
        }

        /// <summary>
        /// Occurs after an e-mail has been sent. The sender is the MailMessage object.
        /// </summary>
        public event EventHandler<EventArgs> EmailFailed;
        private void OnEmailFailed(MailMessage message)
        {
            if (EmailFailed != null)
            {
                EmailFailed(message, new EventArgs());
            }
        }

    }
}
